import { defineStore } from 'pinia';
import { userApi } from '@/api/modules/user.js'

export const useUserStore = defineStore('user', {
  state: () => ({
    userInfo: {},
  }),
  actions: {
    async fetchUserInfo() {
      try {
        const response = await userApi.getUserInfo();
        if (response['sex'] === false || response['sex'] === '' || response['sex'] === null) {
          response['sex'] = '';
        }
        if (response['sex'] === 0 || response['sex'] === '0') {
          response['sex'] = '0';
        }
        if (response['sex'] === 1 || response['sex'] === '1') {
          response['sex'] = '1';
        }
        response['company_id'] = response['company_id'] ? response['company_id'] : ''
        this.userInfo = response;
      } catch (error) {
        console.error('Failed to fetch user info:', error);
      }
    },
  },
  persist: true
});